<?php

namespace console\controllers;

use common\queues\CustomSetSettlementCard;
use common\models\db\AdminPower;
use common\models\db\AdminRole;
use common\models\db\Log;
use common\models\db\User;
use common\queues\LogFengZhangTongSend;
use common\queues\Test;
use common\Tools;
use fztadmin\modules\user\models\forms\CustomBankCard;
use wodrow\yii\rest\models\Route;
use yii\console\Controller;

class TestController extends Controller
{
    public function actionTest1()
    {
        var_dump(YII_BT_TIME - 86400 * 30);
    }

    public function actionTest2()
    {
//        \Yii::$app->cache->delete('test');
        $x = \Yii::$app->cache->getOrSet('test', function (){
            return "dfasdf123";
        }, 3600);
        var_dump($x);
    }

    public function actionTest3()
    {
        var_dump(\Yii::$app->cache->get('test'));
    }

    public function actionTest4($username, $password)
    {
        if (YII_ENV_PROD){
            var_dump("生产环境不能执行该操作");
        }
        $m = new User();
        $m->username = $username;
        $m->password = md5($password);
        $m->created_at = $m->updated_at = YII_BT_TIME;
        $m->status = $m::STATUS_ACTIVE;
        $m->is_admin_super = $m::IS_ADMIN_SUPER_TRUE;
        $m->generateToken();
        if (!$m->save()){
            var_dump($m->errors);
        }
    }

    public function actionTest5()
    {
        if (YII_ENV_PROD){
            var_dump("生产环境不能执行该操作");
        }
        $m = new AdminRole();
        $m->name = "admin";
        $m->created_at = $m->updated_at = YII_BT_TIME;
        $m->max_user_total = 1;
        $m->status = $m::STATUS_ACTIVE;
        if (!$m->save()){
            var_dump($m->errors);
        }
    }

    public function actionTest6()
    {
        var_dump(AdminPower::getAllMenus());
    }

    public function actionTest7()
    {
        if (YII_ENV_PROD){
            var_dump("生产环境不能执行该操作");
        }
        $m = new AdminPower();
        $m->route = "/rbac";
        $m->name = "权限管理";
        $m->created_at = $m->updated_at = YII_BT_TIME;
        $m->sort = 0;
        $m->status = $m::STATUS_ACTIVE;
        $m->icon = "circle-o";
        $m->path = $m->getMenuTreePath();
        if (!$m->save()){
            var_dump($m->errors);
        }
    }

    public function actionTest8()
    {
        if (YII_ENV_PROD){
            var_dump("生产环境不能执行该操作");
        }
        $m = new AdminPower();
        $m->route = "/rbac/user/view";
        $m->name = "用户详情";
        $m->created_at = $m->updated_at = YII_BT_TIME;
        $m->status = $m::STATUS_ACTIVE;
        $m->admin_menu_id = 2;
        if (!$m->save()){
            var_dump($m->errors);
        }
    }

    public function actionTest9()
    {
        $user = User::find()->one();
//        var_dump($user->getAdminMenuFunTree());
        print_r(AdminPower::getAdminMenuFunTree());
//        print_r($user->getAdminPowers());
    }

    public function actionTest10()
    {
        $adminPower = AdminPower::findOne(7);
        $adminPower->save();
        var_dump($adminPower->path);
        foreach (AdminPower::find()->all() as $item) {
            $item->save();
        }
    }

    public function actionTest11()
    {
        $x = AdminPower::getAllRoutes();
        Tools::yiiLog($x);
    }

    public function actionTest12()
    {
        $x = AdminRole::findOne(1);
        var_dump($x->getCheckedAdminMenuFunTree());
    }

    public function actionTest13($acctType = '01')
    {
//        \Yii::$app->fenZhangTong->test();
        $transId = uniqid();
        $params = [
            'acctType' => $acctType,
            'transId' => $transId,//请求业务流水号
            'platCustNo' => "790570452807319553",
        ];
        $fzt = \Yii::$app->fenZhangTong;
        $fzt->IESI111018($params);
        $job = new LogFengZhangTongSend();
        $job->fzt = $fzt;
        \Yii::$app->queue->push($job);
        var_dump($fzt->resp_arr);
    }

    public function actionTest14()
    {
        var_dump(\Yii::$app->fenZhangTong::className());
    }

    public function actionTest15()
    {
        \Yii::$app->zhiLing->sendSms([
            'phones' => "17091551612,18739910648",
            'sms_body' => "330221",
        ]);
    }

    public function actionTest16()
    {
        $mail = \Yii::$app->mailer->compose();
        $mail->setTo("1173957281@qq.com");
        $mail->setSubject("Subject");
        $mail->setTextBody("body");
        echo $mail->send();
    }

    public function actionTest17()
    {
        $transId = uniqid();
        $params = [
            'acctType' => '01',
            'transId' => $transId,//请求业务流水号
            'bgnDate' => '20211210',
            'endDate' => '20211210',
        ];
        $x = \Yii::$app->fenZhangTong->IESI111107($params);
        var_dump($x);
    }

    public function actionTest18($key)
    {
        $testsFile = \Yii::getAlias("@console/config/filedata/fenzhangtongtestdata/个人注册.json");
        $tests = file_get_contents($testsFile);
        $datas = Tools::isJson($tests);
        $transId = uniqid();
        $params = [
            'transId' => $transId,
        ];
        $params = array_merge($params, $datas[$key]);
        $params['notifyUrl'] = \Yii::$app->apiTool->generateUrl('/public/test', ['test' => 321]);
        $params['returnUrl'] = \Yii::$app->apiTool->generateUrl('/public/test', ['test' => 321]);
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111001($params);
        var_dump($fenZhangTong->randKey);
        var_dump($fenZhangTong->serialNo);
        var_dump($r);
        var_dump($fenZhangTong->preEncryptionData);
    }

    public function actionTest19($key)
    {
        $testsFile = \Yii::getAlias("@console/config/filedata/fenzhangtongtestdata/个人注册.json");
        $tests = file_get_contents($testsFile);
        $datas = Tools::isJson($tests);
        $data = $datas[6];
        $card = $data['cardNos'][$key];
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => "788793747507773449",
            'mobileNo' => $data['mobileNo'],
            'openBank' => $card['openBank'],
            'cardNo' => $card['cardNo'],
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111101($params);
        var_dump($transId);
        var_dump($r);
        var_dump($fenZhangTong->preEncryptionData);
    }

    public function actionTest20()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => "788793747507773449",
            'notifyUrl' => THIS_SERVER_BASE_URI, // 通知回调地址
            'returnUrl' => THIS_SERVER_BASE_URI, // 交易完成后页面跳转地址
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111104($params);
        var_dump($transId);
        var_dump($r);
        var_dump($fenZhangTong->preEncryptionData);
    }

    public function actionTest21()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => "788793747507773449",
            'cardNo' => "6212261702008533572",
            'notifyUrl' => \Yii::$app->apiTool->generateUrl("/public/test", [
                'test' => 123456,
            ]), // 通知回调地址
            'returnUrl' => \Yii::$app->apiTool->generateUrl("/public/test", [
                'test' => 123456,
            ]), // 交易完成后页面跳转地址
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111103($params);
        var_dump($transId);
        var_dump($r);
        var_dump($fenZhangTong->preEncryptionData);
        echo($fenZhangTong->technologyMessage);
    }

    public function actionTest22()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => "788793747507773449",
            'cardNo' => "6212261702008533572",
            'oriTransId' => "61bc3ddc61a1b",
            'phoneDynCode' => "862453",
//            'transAmt' => "",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111102($params);
        var_dump($transId);
        var_dump($r);
        var_dump($fenZhangTong->preEncryptionData);
        echo($fenZhangTong->technologyMessage);
    }

    public function actionTest23()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
//            'orderNo' => "JL20211215000002404406",
            'orderNo' => "JL20211203000002250202",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTongStandard;
        $r = $fenZhangTong->IESI111019($params);
        var_dump($r);
    }

    public function actionTest24()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platPayId' => "JL20211210000002319686",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTongStandard;
        $r = $fenZhangTong->IESI111038($params);
        var_dump($r);
    }

    public function actionTest25()
    {
        \Yii::$app->queue->push(new Test());
    }

    public function actionTest26($id)
    {
        $customCard = CustomBankCard::findOne($id);
        $customCard->status = $customCard::STATUS_DEFAULT;
        $customCard->save();
    }

    public function actionTest27($id)
    {
//        \Yii::$app->queue->push(new CustomSetSettlementCard());
        $job = new CustomSetSettlementCard();
        $job->customBankCardId = $id;
        \Yii::$app->queue->push($job);
    }

    public function actionTest28()
    {
        if (YII_ENV_PROD) {
            dd("生产环境不能操作");
        }
        Log::deleteAll();
    }

    public function actionTest29()
    {
        $inpuiries = [];
        $respUseKey = ['bal', 'cashBal', 'floatBal', 'frzCash', 'frzFloat'];
        foreach (['01', '02'] as $k => $v){
            $transId = uniqid();
            $params = [
                'acctType' => $v,
                'transId' => $transId,//请求业务流水号
            ];
            $fzt = \Yii::$app->fenZhangTong;
            $fzt->IESI111018($params);
            $respUseKekArrVal = [];
            foreach ($respUseKey as $k1 => $v1) {
                $respUseKekArrVal[$v1] = $fzt->resp_arr[$v1] ?? 0;
            }
            if ($fzt->ret_code != "00000") {
                foreach ($respUseKey as $k1 => $v1) {
                    $respUseKekArrVal[$v1] = "获取异常:{$fzt->ret_msg}";
                }
            }
            $inpuiries[$v] = array_merge([
                'acctType' => $v,
                'acctTypeDesc' => $fzt->getIESI111018AcctTypeDesc()[$v],
            ], $respUseKekArrVal);
        }
        print_r($inpuiries);
    }

    public function actionTest30($acctType)
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'acctType' => $acctType,
//            'platCustNo' => "",
            'bgnDate' => "20211001",
            'endDate' => "20211223",
            'pageNo' => 1,
            'pageSize' => 1,
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111107($params);
        var_dump($r);
    }

    public function actionTest31($outAcctType, $inAcctType)
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'outAcctType' => $outAcctType,
            'transAmt' => "1.10",
            'inAcctType' => $inAcctType,
            'remark' => "test",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111015($params);
        var_dump($r);
    }

    public function actionTest32($acctType = "04")
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'acctType' => $acctType,
            'transAmt' => "1.10",
            'platCustNo' => "790570452807319553",
            'remark' => "test",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111016($params);
        var_dump($r);
    }

    public function actionTest33()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => "790570452807319553",
            'refundAmt' => "1.0",
            'feeRate' => "0",
            'notifyUrl' => \Yii::$app->apiTool->generateUrl('/route/index'),
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111106($params);
        var_dump($r);
    }

    public function actionTest34()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'transAmt' => "1.0",
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111014($params);
        var_dump($r);
    }

    public function actionTest35()
    {
//        $testsFile = \Yii::getAlias("@console/config/filedata/fenzhangtongtestdata/jlCloseOrderSns.json");
        $testsFile = \Yii::getAlias("@console/config/filedata/fenzhangtongtestdata/jlClose20211220.json");
        $orderSns = file_get_contents($testsFile);
        $orderSnsArr = Tools::isJson($orderSns);
        var_dump(count($orderSnsArr));
        $hasPays = [];
        foreach ($orderSnsArr as $k => $v) {
            $transId = uniqid();
            $params = [
                'transId' => $transId,
                'orderNo' => $v,
            ];
//            $fenZhangTong = \Yii::$app->fenZhangTong;
            $fenZhangTong = \Yii::$app->fenZhangTongStandard;
            $fenZhangTong->IESI111019($params);
            if ($fenZhangTong->ret_code == "00000"){
                if ($fenZhangTong->resp_arr['stat'] == 2){
                    $hasPays[] = $v;
                    var_dump($v);exit;
                }
//                var_dump($v);
            }
        }
        Tools::yiiLog($hasPays);
    }

    public function actionTest36()
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'oriTransId' => "",
            'acctType' => "01",
            'transType' => 1
        ];
        $fenZhangTong = \Yii::$app->fenZhangTong;
        $r = $fenZhangTong->IESI111106($params);
        var_dump($r);
    }
}